package com.toutou.base.bindhelper

import android.graphics.Outline
import android.view.View
import android.view.ViewGroup
import android.view.ViewOutlineProvider
import android.view.animation.Animation
import android.view.animation.Animation.RELATIVE_TO_SELF
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
import androidx.databinding.BindingAdapter


class ViewBDAdapter {
    companion object {
        @JvmStatic
        @BindingAdapter("visible")
        fun setVisibility(view: View, visible: Boolean) {
            view.visibility = Boolean.let {
                if (visible) View.VISIBLE else View.GONE
            }

            view.requestLayout()
            view.invalidate()
        }

        @JvmStatic
        @BindingAdapter("invisible")
        fun setINVisibility(view: View, visible: Boolean) {
            view.visibility = Boolean.let {
                if (visible) View.VISIBLE else View.INVISIBLE
            }
        }

        @JvmStatic
        @BindingAdapter("select")
        fun setSelect(view: View, select: Boolean) {
            view.isSelected = select
        }

        @JvmStatic
        @BindingAdapter("bacSrc")
        fun setBacSrc(view: View, bacSrc: Int) {
            view.setBackgroundResource(bacSrc)
        }

        @JvmStatic
        @BindingAdapter(
            value = ["maxHeight", "maxWidth", "minHeight", "minWidth"],
            requireAll = false
        )
        fun viewLimitParam(
            view: View,
            maxHeight: Int,
            maxWidth: Int,
            minHeight: Int,
            minWidth: Int
        ) {
            val dp = view.context.resources.displayMetrics.density
            val finalMaxHeight = (dp * maxHeight).toInt()
            val finalMaxWidth = (dp * maxWidth).toInt()
            val finalMinHeight = (dp * minHeight).toInt()
            val finalMinWidth = (dp * minWidth).toInt()

            view.viewTreeObserver.addOnGlobalLayoutListener {
                val params = view.layoutParams
                val height = view.height
                val width = view.width
                println("width:$width")
                if (finalMaxHeight in 1..<height) {
                    params.height = finalMaxHeight
                    view.invalidate()
                }
                if (finalMaxWidth in 1..<width) {
                    params.width = finalMaxWidth
                    view.invalidate()
                }
                if (finalMinHeight > 0 && height < finalMinHeight) {
                    params.height = finalMinHeight
                    view.invalidate()
                }
                if (finalMinWidth > 0 && width < finalMinWidth) {
                    params.width = finalMinWidth
                    view.invalidate()
                }
            }
        }

        //        设置高度
        @JvmStatic
        @BindingAdapter("layoutHeight")
        fun setLayoutHeight(view: View, layoutHeight: Int) {
            val dp = view.context.resources.displayMetrics.density
            val finalHeight = (dp * layoutHeight).toInt()
            val params = view.layoutParams
            if (layoutHeight == ViewGroup.LayoutParams.WRAP_CONTENT || layoutHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
                params.height = layoutHeight
            } else {
                params.height = finalHeight
            }
            view.layoutParams = params
            view.invalidate()
        }

        //        设置长度
        @JvmStatic
        @BindingAdapter("layoutWidth")
        fun setLayoutWidth(view: View, layoutWidth: Int) {
            val dp = view.context.resources.displayMetrics.density
            val finalWidth = (dp * layoutWidth).toInt()
            val params = view.layoutParams
            if (layoutWidth == ViewGroup.LayoutParams.WRAP_CONTENT || layoutWidth == ViewGroup.LayoutParams.MATCH_PARENT) {
                params.width = layoutWidth
            } else {
                params.width = finalWidth
            }
            view.layoutParams = params
            view.invalidate()
        }

        //        设置marginHorizon
        @JvmStatic
        @BindingAdapter("layoutMarginHorizon")
        fun setLayoutMarginHorizon(view: View, layoutMarginHorizon: Int) {
            val dp = view.context.resources.displayMetrics.density
            val finalMargin = (dp * layoutMarginHorizon).toInt()
            val params = view.layoutParams as ViewGroup.MarginLayoutParams
            params.marginStart = finalMargin
            params.marginEnd = finalMargin
            view.layoutParams = params
            view.invalidate()
        }

        //        设置圆角
        @JvmStatic
        @BindingAdapter("conorRadius")
        fun setConorRadius(view: View, conorRadius: Float) {
            view.outlineProvider = object : ViewOutlineProvider() {
                override fun getOutline(view: View?, outline: Outline?) {
                    outline?.setRoundRect(
                        0,
                        0,
                        view!!.width,
                        view.height,
                        view.context.resources.displayMetrics.density * conorRadius
                    )
                }
            }
            view.clipToOutline = true
        }

        //        设置margin
        @JvmStatic
        @BindingAdapter("layoutMarginStart")
        fun setLayoutMargin(view: View, layoutMarginStart: Int) {
            val dp = view.context.resources.displayMetrics.density
            val finalMargin = (dp * layoutMarginStart).toInt()
            val params = view.layoutParams as ViewGroup.MarginLayoutParams
            params.marginStart = finalMargin
            view.layoutParams = params
            view.invalidate()
        }

        //        原地旋转动画
        @JvmStatic
        @BindingAdapter("rotationStart")
        fun rotationStart(view: View, rotationStart: Boolean) {
            if (rotationStart) {
//                定义一个转圈的动画
                view.startAnimation(RotateAnimation(0f, 360f, RELATIVE_TO_SELF, 0.5f, RELATIVE_TO_SELF, 0.5f).apply {
                    duration = 1000
                    repeatCount = Animation.INFINITE
                    interpolator = LinearInterpolator()
                })
            } else {
                view.clearAnimation()//清除当前的所有动画。这可以确保视图不再受之前动画的影响，从而允许新的动画正常启动。

            }

        }

    }
}